package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.form.AdviceForm;
import io.renren.modules.hos.service.HosUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.hos.entity.MedicalAdviceEntity;
import io.renren.modules.hos.service.MedicalAdviceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-13 11:19:14
 */
@RestController
@RequestMapping("hos/medicalAdvice")
public class MedicalAdviceController {
    @Autowired
    private MedicalAdviceService medicalAdviceService;
    @Autowired
    private HosUserService hosUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hos:medicaladvice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = medicalAdviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:medicaladvice:info")
    public R info(@PathVariable("id") Long id){
		MedicalAdviceEntity medicalAdvice = medicalAdviceService.getById(id);

        return R.ok().put("medicalAdvice", medicalAdvice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:medicaladvice:save")
    public R save(@RequestBody MedicalAdviceEntity medicalAdvice){
		medicalAdviceService.save(medicalAdvice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:medicaladvice:update")
    public R update(@RequestBody MedicalAdviceEntity medicalAdvice){
		medicalAdviceService.updateById(medicalAdvice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:medicaladvice:delete")
    public R delete(@RequestBody Long[] ids){
		medicalAdviceService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 添加医嘱
     */
    @PostMapping("/add")
    public R addAdvice(@RequestBody AdviceForm adviceForm){
        HosUserEntity user = hosUserService.getById(ShiroUtils.getHosUserId());
        if (user.getAuthIdentity() != 2){
            return R.error().put("msg", "您无权限");
        }else {
            Long doctorId = hosUserService.selectDoctorId(user.getId());
            medicalAdviceService.addAdvice(doctorId, adviceForm);
            return R.ok().put("msg", "添加成功");
        }


    }

    /**
     * 病人查看医嘱历史记录
     */
    @PostMapping("/advicePatient")
    public R selectAdvicePatient(@RequestBody Map<String, Object> params){
        HosUserEntity userEntity = hosUserService.getById(ShiroUtils.getHosUserId());
        if (userEntity.getAuthIdentity() != 1){
            return R.error().put("msg", "您无权限");
        }else {
            Long patientId = hosUserService.selectPatientId(userEntity.getId());
            PageUtils page = medicalAdviceService.queryByPatientId(patientId, params);
            return R.ok().put("page", page);
        }
    }

    /**
     *  医生查看自己开的医嘱,并分页
     */
    @PostMapping("/adviceDoctor")
    public R selectAdviceDoctor(@RequestBody Map<String, Object> params){
        HosUserEntity user = hosUserService.getById(ShiroUtils.getHosUserId());
        if (user.getAuthIdentity() != 2){
            return R.error().put("msg", "您无权限");
        }else {
            Long doctorId = hosUserService.selectDoctorId(user.getId());
            PageUtils page = medicalAdviceService.queryBuDoctorId(doctorId, params);
            return R.ok().put("page", page);
        }
    }

}
